//
//  BaseApi.m
//  Api
//
//  Created by CPX on 2018/5/29.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "BaseApi.h"

NSErrorDomain APPResponseError = @"app_response_error";

/**
 接口基类
 @author yzl
 */
@implementation BaseApi

+ (instancetype)standard{
    static dispatch_once_t onceToken;
    static BaseApi *mgr = nil;
    dispatch_once(&onceToken, ^{
        mgr = [BaseApi new];
        [mgr _init];
    });
    return mgr;
}

- (void)_init{
    self.auth  = @"";
    self.language = [NSLocale preferredLanguages][0];
    
//    self.version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    self.version = @"2.2";
    
}

+ (void)registerApp{
//    [BaseApi standard].auth = @"";
    Class mineTool = NSClassFromString(@"MineTool");
    if (mineTool) {
        SEL sel1 = NSSelectorFromString(@"sharedTool");
        SEL sel2 = NSSelectorFromString(@"userToken");
        id obj = [mineTool performSelector:sel1];
        NSString *token = [obj performSelector:sel2];
        [BaseApi standard].auth = [NSString stringWithFormat:@"Token %@",token];
    }
}

+ (BOOL)dealWithResponse:(id)response{
    if (response && response[@"code"] && [response[@"code"] integerValue] == 0) {
        return true;
    }
    return false;
}

+ (NSDictionary*)dealError:(NSError *)error{
    if (error && error.userInfo[@"com.alamofire.serialization.response.error.data"]) {
        return [NSJSONSerialization JSONObjectWithData:error.userInfo[@"com.alamofire.serialization.response.error.data"] options:NSJSONReadingAllowFragments error:nil];
    }
    return nil;
}

+ (NSMutableDictionary*)header{
    NSMutableDictionary *p = [NSMutableDictionary dictionaryWithObjectsAndKeys:[BaseApi standard].language,@"language",[BaseApi standard].version,@"version", nil];
    if ([BaseApi standard].auth.length>7) {
        p[@"Authorization"] = [BaseApi standard].auth;
    }
    return p;
}

@end
